package cn.iocoder.yudao.module.product.controller.admin.skuofferpricerelation;

import cn.iocoder.yudao.module.product.convert.skuofferpricerelation.SkuOfferPriceRelationConvert;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.skuofferpricerelation.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.skuofferpricerelation.SkuOfferPriceRelationDO;
import cn.iocoder.yudao.module.product.service.skuofferpricerelation.SkuOfferPriceRelationService;

@Tag(name = "管理后台 - sku与报价关系")
@RestController
@RequestMapping("/product/sku-offer-price-relation")
@Validated
public class SkuOfferPriceRelationController {

    @Resource
    private SkuOfferPriceRelationService skuOfferPriceRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建sku与报价关系")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:create')")
    public CommonResult<Long> createSkuOfferPriceRelation(@Valid @RequestBody SkuOfferPriceRelationSaveReqVO createReqVO) {
        return success(skuOfferPriceRelationService.createSkuOfferPriceRelation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新sku与报价关系")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:update')")
    public CommonResult<Boolean> updateSkuOfferPriceRelation(@Valid @RequestBody SkuOfferPriceRelationSaveReqVO updateReqVO) {
        skuOfferPriceRelationService.updateSkuOfferPriceRelation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除sku与报价关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:delete')")
    public CommonResult<Boolean> deleteSkuOfferPriceRelation(@RequestParam("id") Long id) {
        skuOfferPriceRelationService.deleteSkuOfferPriceRelation(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得sku与报价关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:query')")
    public CommonResult<SkuOfferPriceRelationRespVO> getSkuOfferPriceRelation(@RequestParam("id") Long id) {
        SkuOfferPriceRelationDO skuOfferPriceRelation = skuOfferPriceRelationService.getSkuOfferPriceRelation(id);
        return success(BeanUtils.toBean(skuOfferPriceRelation, SkuOfferPriceRelationRespVO.class));
    }

    @GetMapping("/find")
    @Operation(summary = "获得sku与报价关系")
    @Parameter(name = "skuId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:query')")
    public CommonResult<List<SkuOfferPriceRelationRespVO>> getSkuOfferPriceRelationBySkuId(@RequestParam("skuId") Long skuId) {
        List<SkuOfferPriceRelationRespVO> skuOfferPriceRelation = skuOfferPriceRelationService.getSkuOfferPriceRelationListBySkuId(skuId);
        return success(skuOfferPriceRelation);
    }

    @GetMapping("/page")
    @Operation(summary = "获得sku与报价关系分页")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:query')")
    public CommonResult<PageResult<SkuOfferPriceRelationRespVO>> getSkuOfferPriceRelationPage(@Valid SkuOfferPriceRelationPageReqVO pageReqVO) {
        PageResult<SkuOfferPriceRelationDO> pageResult = skuOfferPriceRelationService.getSkuOfferPriceRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuOfferPriceRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出sku与报价关系 Excel")
    @PreAuthorize("@ss.hasPermission('product:sku-offer-price-relation:export')")
    @OperateLog(type = EXPORT)
    public void exportSkuOfferPriceRelationExcel(@Valid SkuOfferPriceRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuOfferPriceRelationDO> list = skuOfferPriceRelationService.getSkuOfferPriceRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "sku与报价关系.xls", "数据", SkuOfferPriceRelationRespVO.class,
                        BeanUtils.toBean(list, SkuOfferPriceRelationRespVO.class));
    }

}